package com.wlw.admin.openeye.ui.adapter

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Build
import android.support.v4.app.ActivityCompat
import android.support.v4.app.ActivityOptionsCompat
import android.support.v4.content.ContextCompat
import android.support.v4.util.Pair
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.hazz.kotlinmvp.view.recyclerview.ViewHolder
import com.hazz.kotlinmvp.view.recyclerview.adapter.CommonAdapter
import com.wlw.admin.openeye.Constants
import com.wlw.admin.openeye.R
import com.wlw.admin.openeye.durationFormat
import com.wlw.admin.openeye.mvp.model.bean.HomeBean
import com.wlw.admin.openeye.ui.activity.VideoDetailActivity

class WatchHistoryAdapter(context: Context, dataList: ArrayList<HomeBean.Issue.Item>) : CommonAdapter<HomeBean.Issue.Item>(context, dataList, R.layout.item_video_small_card) {
    override fun bindData(holder: ViewHolder, data: HomeBean.Issue.Item, position: Int) {
        with(holder) {
            setText(R.id.tv_title, data.data?.title!!)
            setText(R.id.tv_tag, "#${data.data.category}/${durationFormat(data.data.duration)}")
            setImagePath(R.id.iv_video_small_card, object : ViewHolder.HolderImageLoader(data.data.cover.detail) {
                override fun loadImage(iv: ImageView, path: String) {
                    Glide.with(mContext).load(path)
                            .transition(DrawableTransitionOptions().crossFade())
                            .into(iv)

                }
            })
            getView<TextView>(R.id.tv_title).setTextColor(ContextCompat.getColor(mContext, R.color.color_black))
            setOnItemClickListener(listener = View.OnClickListener {
                goToVideoPlayer(mContext as Activity, getView<ImageView>(R.id.iv_video_small_card), data)
            })
        }
    }

    private fun goToVideoPlayer(activity: Activity, view: ImageView, data: HomeBean.Issue.Item) {
        val intent = Intent(activity, VideoDetailActivity::class.java)
        intent.putExtra(Constants.BUNDLE_VIDEO_DATA, data)
        intent.putExtra(VideoDetailActivity.TRANSITION, true)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            val pair=Pair<View,String>(view,VideoDetailActivity.IMG_TRANSITION)
            val activityOptions=ActivityOptionsCompat.makeSceneTransitionAnimation(activity,pair)
            ActivityCompat.startActivity(activity,intent,activityOptions.toBundle())
        }else{
            activity.startActivity(intent)
            activity.overridePendingTransition(R.anim.anim_in,R.anim.anim_out)
        }
    }
}